﻿using PPIDesktopClient.Entities;
using PPIDesktopClient.General;
using PPIDesktopClient.Logic;
using PPIProxy;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using VMind.Global;

namespace PPIDesktopClient
{
    public partial class frmAssetQuery : frmDialogBase
    {
        #region Private Members
        private Int32 m_QueryAssetId = Int32.MinValue;
        #endregion

        #region Public Properties
        public Int32 QueryAssetId
        {
            get { return m_QueryAssetId; }
            set { m_QueryAssetId = value; }
        }
        #endregion

        public frmAssetQuery()
        {
            InitializeComponent();
        }

        private void frmAssetQuery_Load(object sender, EventArgs e)
        {
            try
            {
                this.tbcAssetQuery.SelectedTab = tbiCriteria;
                LoadCombos();
                if (m_QueryAssetId != Int32.MinValue)
                    LoadData();
            }
            catch (Exception ex)
            {
                ErrorManager.Log("frmAssetQuery.frmAssetQuery_Load()", ex.Source, ex.Message, ex.StackTrace);
            }
        }

        private void LoadCombos()
        {
            PPIDesktopClient.General.UIHelper.FillDropDown(ddlType, LUtility_Query_Type.SelectAllIdName(), "Name", "Utility_Query_TypeId", "Select an option...");
            PPIDesktopClient.General.UIHelper.FillDropDown(ddlTextToShow, LLookup.SelectAllbyType("EUtility_QueryAssetTextShow"),typeof(ELookup), "LookupText", "LookupValue", "Select an option...");
            lstYears.BeginUpdate();
            for (Int32 _Year = DateTime.Now.Year; _Year > 1950; _Year--)
                lstYears.Items.Add(new ListBoxItem(_Year.ToString(), _Year.ToString()));
            lstYears.EndUpdate();
            PPIDesktopClient.General.UIHelper.FillListBox(lstTopTypes, LXMissionStructures_TopType.SelectAllIdName(), "Name", "XMissionStructures_TopTypeId", "");
            PPIDesktopClient.General.UIHelper.FillListBox(lstPoleTopTypes, LXMissionStructures_TopType.SelectAllIdNameList(), typeof(EXMissionStructures_TopType), "Name", "XMissionStructures_TopTypeId", "");
            PPIDesktopClient.General.UIHelper.FillListBox(lstPoleSpecies, LXMissionStructures_PoleSpecies.SelectAllIdName(), typeof(EXMissionStructures_PoleSpecies), "Name", "XMissionStructures_PoleSpeciesId", "");
            PPIDesktopClient.General.UIHelper.FillListBox(lstProjects, LUtility_Project.SelectAll(), "Name", "Utility_ProjectId", "");
            PPIDesktopClient.General.UIHelper.FillListBox(lstPoleTreatments, LXMissionStructures_PoleTreatments.SelectAllIdName(), typeof(EXMissionStructures_PoleTreatment), "Name", "XMissionStructures_PoleTreatmentId", "");
            PPIDesktopClient.General.UIHelper.FillListBox(lstPoleClasses, LXMissionStructures_PoleClass.SelectAllIdName(), typeof(EXMissionStructures_PoleClass), "Name", "XMissionStructures_PoleClassId", "");
            PPIDesktopClient.General.UIHelper.FillListBox(lstVendors, LVendor.SelectAllIdName(),typeof(EVendor), "Name", "VendorId", "");
            PPIDesktopClient.General.UIHelper.FillListBox(lstAssets, LUtility_Asset_XMissionStructure.SelectAllIdName(), "Utility_Asset_XMissionStructureId", "Utility_Asset_XMissionStructureId", "");
            

            //Loading icon color dropdown
            string[] iconColors = Enum.GetNames(typeof(IconColor));
            ddlColorIcons.Items.Clear();
            ddlColorIcons.Items.Add(new ListBoxItem("Select an option...", "0"));
            foreach (string c in iconColors)
            {
                ddlColorIcons.Items.Add(new ListBoxItem(c, c));    
            }
        }

        private void LoadData()
        {
            EUtility_QueryAsset _Query = LUtility_QueryAsset.SelectCRUD(m_QueryAssetId);
            txtName.Text = _Query.Name;
            ddlColorIcons.SelectedIndex = PPIDesktopClient.General.UIHelper.GetItemIndex(ddlColorIcons, _Query.Color);
            if (ddlColorIcons.SelectedIndex == -1)
                ddlColorIcons.SelectedIndex = 0;
            ddlTextToShow.SelectedIndex = PPIDesktopClient.General.UIHelper.GetItemIndex(ddlTextToShow, ((Int32)_Query.TextToShow).ToString());
            if (ddlTextToShow.SelectedIndex == -1)
                ddlColorIcons.SelectedIndex = 0;


            ddlType.SelectedIndex = PPIDesktopClient.General.UIHelper.GetItemIndex(ddlType, _Query.Utility_Query_Type.Utility_Query_TypeId.ToString());
            chkShowLineParents.Checked = _Query.ShowLineParents;
            foreach (DataRow _Dr in LUtility_QueryAsset.SelectYears(m_QueryAssetId).Rows)
                lstYears.SelectedIndices.Add(PPIDesktopClient.General.UIHelper.GetItemIndex(lstYears, ConvertHelper.Tostring(_Dr["Year"])));
            foreach (DataRow _Dr in LUtility_QueryAsset.SelectTopTypes(m_QueryAssetId).Rows)
                lstTopTypes.SelectedIndices.Add(PPIDesktopClient.General.UIHelper.GetItemIndex(lstTopTypes, ConvertHelper.Tostring(_Dr["XMissionStructures_TopTypeFk"])));
            foreach (DataRow _Dr in LUtility_QueryAsset.SelectPoleTopTypes(m_QueryAssetId).Rows)
                lstPoleTopTypes.SelectedIndices.Add(PPIDesktopClient.General.UIHelper.GetItemIndex(lstPoleTopTypes, ConvertHelper.Tostring(_Dr["XMissionStructures_PoleTopTypeFk"])));
            foreach (DataRow _Dr in LUtility_QueryAsset.SelectPoleSpecies(m_QueryAssetId).Rows)
                lstPoleSpecies.SelectedIndices.Add(PPIDesktopClient.General.UIHelper.GetItemIndex(lstPoleSpecies, ConvertHelper.Tostring(_Dr["XMissionStructures_PoleSpeciesFk"])));
            foreach (DataRow _Dr in LUtility_QueryAsset.SelectProjects(m_QueryAssetId).Rows)
                lstProjects.SelectedIndices.Add(PPIDesktopClient.General.UIHelper.GetItemIndex(lstProjects, ConvertHelper.Tostring(_Dr["Utility_ProjectFk"])));
            foreach (DataRow _Dr in LUtility_QueryAsset.SelectPoleTreatments(m_QueryAssetId).Rows)
                lstPoleTreatments.SelectedIndices.Add(PPIDesktopClient.General.UIHelper.GetItemIndex(lstPoleTreatments, ConvertHelper.Tostring(_Dr["XMissionStructures_PoleTreatmentFk"])));
            foreach (DataRow _Dr in LUtility_QueryAsset.SelectPoleClasses(m_QueryAssetId).Rows)
                lstPoleClasses.SelectedIndices.Add(PPIDesktopClient.General.UIHelper.GetItemIndex(lstPoleClasses, ConvertHelper.Tostring(_Dr["XMissionStructures_PoleClassFk"])));
            foreach (DataRow _Dr in LUtility_QueryAsset.SelectVendors(m_QueryAssetId).Rows)
                lstVendors.SelectedIndices.Add(PPIDesktopClient.General.UIHelper.GetItemIndex(lstVendors, ConvertHelper.Tostring(_Dr["VendorFk"])));
            foreach (DataRow _Dr in LUtility_QueryAsset.SelectAssets(m_QueryAssetId).Rows)
                lstAssets.SelectedIndices.Add(PPIDesktopClient.General.UIHelper.GetItemIndex(lstAssets, ConvertHelper.Tostring(_Dr["Utility_Asset_XMissionStructureFk"])));
        }

        private bool ValidateCriteria()
        {
            bool _IsValid = false;
            _IsValid |= (lstYears.SelectedIndices.Count > 0);
            _IsValid |= (lstTopTypes.SelectedIndices.Count > 0);
            _IsValid |= (lstPoleTopTypes.SelectedIndices.Count > 0);
            _IsValid |= (lstPoleSpecies.SelectedIndices.Count > 0);
            _IsValid |= (lstProjects.SelectedIndices.Count > 0);
            _IsValid |= (lstPoleTreatments.SelectedIndices.Count > 0);
            _IsValid |= (lstPoleClasses.SelectedIndices.Count > 0);
            _IsValid |= (lstVendors.SelectedIndices.Count > 0);
            _IsValid |= (lstAssets.SelectedIndices.Count > 0);
            return _IsValid;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                RFV_Criteria.Tag = !ValidateCriteria();
                RFV_Criteria.Visible = (bool)RFV_Criteria.Tag;
                if (ValidateAll() && !(bool)RFV_Criteria.Tag)
                {
                    EUtility_QueryAsset _Query = new EUtility_QueryAsset();
                    _Query.Utility_QueryAssetId = m_QueryAssetId;
                    _Query.Name = txtName.Text;
                    _Query.TextToShow = (EUtility_QueryAssetTextShow)ConvertHelper.ToInt32(((ListBoxItem)ddlTextToShow.SelectedItem).Value);
                    _Query.Color = ((ListBoxItem)ddlColorIcons.SelectedItem).Value;
                    _Query.Utility_Query_Type = new EUtility_Query_Type(Convert.ToInt32(((ListBoxItem)ddlType.SelectedItem).Value));
                    _Query.ShowLineParents = chkShowLineParents.Checked;

                    _Query.Years = new Int32[lstYears.SelectedIndices.Count];
                    for (Int32 _I = 0; _I < lstYears.SelectedIndices.Count; _I++)
                        _Query.Years[_I] = ConvertHelper.ToInt32(((ListBoxItem)lstYears.Items[lstYears.SelectedIndices[_I]]).Value);
                    _Query.XMissionStructures_TopTypes = new EXMissionStructures_TopType[lstTopTypes.SelectedIndices.Count];
                    for (Int32 _I = 0; _I < lstTopTypes.SelectedIndices.Count; _I++)
                        _Query.XMissionStructures_TopTypes[_I] = new EXMissionStructures_TopType(ConvertHelper.ToInt32(((ListBoxItem)lstTopTypes.Items[lstTopTypes.SelectedIndices[_I]]).Value));
                    _Query.XMissionStructures_PoleTopTypes = new EXMissionStructures_PoleTopType[lstPoleTopTypes.SelectedIndices.Count];
                    for (Int32 _I = 0; _I < lstPoleTopTypes.SelectedIndices.Count; _I++)
                        _Query.XMissionStructures_PoleTopTypes[_I] = new EXMissionStructures_PoleTopType(ConvertHelper.ToInt32(((ListBoxItem)lstPoleTopTypes.Items[lstPoleTopTypes.SelectedIndices[_I]]).Value));
                    _Query.XMissionStructures_PoleSpecies = new EXMissionStructures_PoleSpecies[lstPoleSpecies.SelectedIndices.Count];
                    for (Int32 _I = 0; _I < lstPoleSpecies.SelectedIndices.Count; _I++)
                        _Query.XMissionStructures_PoleSpecies[_I] = new EXMissionStructures_PoleSpecies(ConvertHelper.ToInt32(((ListBoxItem)lstPoleSpecies.Items[lstPoleSpecies.SelectedIndices[_I]]).Value));
                    _Query.Utility_Projects = new EUtility_Project[lstProjects.SelectedIndices.Count];
                    for (Int32 _I = 0; _I < lstProjects.SelectedIndices.Count; _I++)
                        _Query.Utility_Projects[_I] = new EUtility_Project(ConvertHelper.ToInt32(((ListBoxItem)lstProjects.Items[lstProjects.SelectedIndices[_I]]).Value));
                    _Query.XMissionStructures_PoleTreatments = new EXMissionStructures_PoleTreatment[lstPoleTreatments.SelectedIndices.Count];
                    for (Int32 _I = 0; _I < lstPoleTreatments.SelectedIndices.Count; _I++)
                        _Query.XMissionStructures_PoleTreatments[_I] = new EXMissionStructures_PoleTreatment(ConvertHelper.ToInt32(((ListBoxItem)lstPoleTreatments.Items[lstPoleTreatments.SelectedIndices[_I]]).Value));
                    _Query.XMissionStructures_PoleClasses = new EXMissionStructures_PoleClass[lstPoleClasses.SelectedIndices.Count];
                    for (Int32 _I = 0; _I < lstPoleClasses.SelectedIndices.Count; _I++)
                        _Query.XMissionStructures_PoleClasses[_I] = new EXMissionStructures_PoleClass(ConvertHelper.ToInt32(((ListBoxItem)lstPoleClasses.Items[lstPoleClasses.SelectedIndices[_I]]).Value));
                    _Query.Vendors = new EVendor[lstVendors.SelectedIndices.Count];
                    for (Int32 _I = 0; _I < lstVendors.SelectedIndices.Count; _I++)
                        _Query.Vendors[_I] = new EVendor(ConvertHelper.ToInt32(((ListBoxItem)lstVendors.Items[lstVendors.SelectedIndices[_I]]).Value));
                    _Query.Utility_Asset_XMissionStructures = new EUtility_Asset_XMissionStructure[lstAssets.SelectedIndices.Count];
                    for (Int32 _I = 0; _I < lstAssets.SelectedIndices.Count; _I++)
                        _Query.Utility_Asset_XMissionStructures[_I] = new EUtility_Asset_XMissionStructure(ConvertHelper.ToInt32(((ListBoxItem)lstAssets.Items[lstAssets.SelectedIndices[_I]]).Value));

                    LUtility_QueryAsset.Save(_Query);
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else if (!ValidateAll())
                    tbcAssetQuery.SelectedTab = tbiSetup;
                else
                    tbcAssetQuery.SelectedTab = tbiCriteria;
            }
            catch (Exception ex)
            {
                ErrorManager.Log("frmAssetQuery.btnSave_Click()", ex.Source, ex.Message, ex.StackTrace);
            }
        }
    }
}
